/*
 * Copyright ©2015-2023 Jaemon. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.cloud.alarm.dinger.bytetalk.entity;

import com.cloud.alarm.dinger.bytetalk.entity.enums.ByteTalkMsgType;
import com.cloud.alarm.dinger.support.sign.SignBase;
import com.cloud.alarm.dinger.support.sign.SignResult;
import lombok.Getter;

import java.io.Serial;
import java.io.Serializable;
import java.util.Map;

/**
 * 飞书-消息类型-文本类型
 *
 * @author shuai.zhou
 * @since 1.0
 */
@Getter
public class ByteText extends ByteTalkMessage {

    @Serial
    private static final long serialVersionUID = 7466288416938801568L;

    /**
     * 消息类型
     */
    private final String msg_type;
    /**
     * 消息内容
     */
    private final Content content;
    /**
     * 签名-时间戳
     */
    private String timestamp;
    /**
     * 签名-秘钥
     */
    private String sign;

    public ByteText(Content content) {
        this.msg_type = ByteTalkMsgType.TEXT.getType();
        this.content = content;
    }

    @Override
    public void signAttributes(SignBase sign) {
        if (!(sign instanceof SignResult signResult)) {
            return;
        }
        this.timestamp = String.valueOf(signResult.getTimestamp());
        this.sign = signResult.getSign();
    }

    @Getter
    public static class Content implements Serializable {

        @Serial
        private static final long serialVersionUID = -5008373568733371559L;

        /**
         * 文本内容，最长不超过2048个字节，必须是utf8编码
         */
        private String text;

        public Content() {
        }

        public Content(String text) {
            this.text = text;
        }
    }

    @Override
    public void transfer(Map<String, Object> params) {
        this.content.text = replaceContent(this.content.text, params);
    }

}